package com.zengqingfa.designpattern.behavior.observer.standard;

import java.util.ArrayList;
import java.util.List;

/**
 *
 * @fileName: ConcreteSubject
 * @author: zengqf3
 * @date: 2021-4-23 15:06
 * @description: 具体主题（被观察者）
 */
public class ConcreteSubject implements Subject {
    /**
     * 观察者列表
     */
    private List<Observer> observerList;

    public ConcreteSubject() {
        observerList = new ArrayList<>();
    }

    @Override
    public void addObserver(Observer observer) {
        observerList.add(observer);
    }

    @Override
    public void removeObserver(Observer observer) {
        observerList.remove(observer);
    }

    @Override
    public void notifyObserver() {
        for (Observer observer : observerList) {
            observer.update("standard标准观察者");
        }
    }

    /**
     * 触发更新
     */
    public void triggle() {
        System.out.println("被观察者事件发生改变");
        this.notifyObserver();
    }
}
